from rest_framework import mixins
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, DestroyModelMixin
from rest_framework.viewsets import ViewSet, GenericViewSet, ReadOnlyModelViewSet, ModelViewSet

from .serializers import BookInfoSerializer
from booktest.models import BookInfo



class BookInfoViewSet(ModelViewSet):
    """使用ModelViewSet视图集获取所有或单一图书信息接口"""
    # 指定查询集
    queryset = BookInfo.objects.all()
    # 指定序列化器的类
    serializer_class = BookInfoSerializer

    @action(methods=['get'], detail=False)
    def latest(self, request):
        """
        返回最新的图书信息
        """
        book = BookInfo.objects.latest('id')
        serializer = self.get_serializer(book)
        return Response(serializer.data)

    @action(methods=['put'], detail=True)
    def read(self, request, pk):
        """
        修改图书的阅读量数据
        """
        book = self.get_object()
        book.bread = request.data.get('read')
        book.save()
        serializer = self.get_serializer(book)
        return Response(serializer.data)


# ReadOnlyModelViewSet 继承自GenericViewSet，同时包括了ListModelMixin、RetrieveModelMixin。
# class BookInfoViewSet(ReadOnlyModelViewSet, GenericViewSet):
#     """使用BookInfoViewSet＋　mixin 视图集获取所有或单一图书信息接口"""
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器的类
#     serializer_class = BookInfoSerializer


# class BookInfoViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
#     """使用BookInfoViewSet＋　mixin 视图集获取所有或单一图书信息接口"""
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器的类
#     serializer_class = BookInfoSerializer


# class BookInfoViewSet(GenericViewSet):
#     """使用BookInfoViewSet视图集获取所有或单一图书信息接口"""
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器的类
#     serializer_class = BookInfoSerializer
#
#     def list(self, request):
#         books = self.get_queryset()
#         serializer = self.get_serializer(books, many=True)
#         return Response(serializer.data)
#
#     def retrieve(self, request, pk):
#         book = self.get_object()
#         serializer = self.get_serializer(book)
#         return Response(serializer.data)


class BookDetailView(RetrieveAPIView):
    """使用GenericAPIView实现获得单一图书信息接口"""
    # 1.指定查询集
    queryset = BookInfo.objects.all()

    # 2.指定序列化器的类
    serializer_class = BookInfoSerializer


class BookListView(ListAPIView):
    """使用GenericAPIView实现获得单一图书信息接口"""
    # 1 .指定查询集
    queryset = BookInfo.objects.all()

    # 2 .指定序列化器的类
    serializer_class = BookInfoSerializer


# class BookListView(ListModelMixin, GenericAPIView):
#     """使用GenericAPIView实现获得单一图书信息接口"""
#     # 1 .指定查询集
#     queryset = BookInfo.objects.all()
#
#     # 2 .指定序列化器的类
#     serializer_class = BookInfoSerializer
#
#     def get(self, request):
#         return self.list(request)


# class BookDetailView(RetrieveModelMixin,GenericAPIView):
#     """使用GenericAPIView实现获得单一图书信息接口"""
#     # 1.指定查询集
#     queryset = BookInfo.objects.all()
#
#     # 2.指定序列化器的类
#     serializer_class = BookInfoSerializer
#
#     # 3.响应
#     def get(self, request, pk):
#
#         return self.retrieve(request, pk)


# class BookListView(GenericAPIView):
#     """使用GenericAPIView实现获得所有图书信息接口"""
#     # 1 .指定查询集
#     queryset = BookInfo.objects.all()
#
#     # 2 .指定序列化器的类
#     serializer_class = BookInfoSerializer
#
#     def get(self, request):
#         # 1.获取查询集
#         qs = self.get_queryset()
#         # 2.序列化
#         serializer = self.get_serializer(qs, many=True)
#         # 3.响应
#         return Response(serializer.data)
#
#
# class BookDetailView(GenericAPIView):
#     """使用GenericAPIView实现获得单一图书信息接口"""
#     # 1.指定查询集
#     queryset = BookInfo.objects.all()
#
#     # 2.指定序列化器的类
#     serializer_class = BookInfoSerializer
#
#     # 3.响应
#     def get(self, request, pk):
#         # 1.获取指定pk的模型对象
#         book = self.get_object()
#         # 2.序列化
#         serializer = self.get_serializer(book)
#         # 3.响应
#         return Response(serializer.data)


class BooksAPIView(APIView):
    """使用APIView实现获取所有图书信息接口"""

    def get(self, request):
        """
        GET/books/
        :param request:是ＤＲＦ中Request类型的请求对象
        :return: 响应ＪＳＯＮ，响应对象是ＤＲＦ中的Request类型对象
        """
        # 1.获取查询集
        qs = BookInfo.objects.all()

        # 2.序列化
        serializer = BookInfoSerializer(qs, many=True)

        # 3.返回响应
        return Response(serializer.data)